package com.llc.uh.quiz;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StoreQuizDataServlet extends HttpServlet
{

    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException
    {

        Key key;

        /**
         * Deletes the data in the datastore. To be used when adding new/changed
         * questions
         */
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

        Query query = new Query("Quiz");
        List<Entity> questions = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(20));

        for (Entity question : questions)
        {
            key = question.getKey();
            datastore.delete(key);
        }

        /**
         * Stores data in the Quiz datastore. To be used when adding new/changed
         * questions. Now we do it every time we change questions/answers during
         * development. Later will use a UI to this.
         */

        Entity question = new Entity("Quiz", 1);
        question.setProperty("chinese", "1. 用 心 照 料 好 眼 前 該 做 的 事 ， 才 可 希 望 有 個 無 悔 的 將 來 。");
        question.setProperty("instruction", "(This passage is taken from a newspaper essay.)");
        question.setProperty("question", "According to the author, the best way to avoid regret is:");
        question.setProperty("a", "a) not to make the same mistake twice.");
        question.setProperty("b", "b) not to engage in efforts beyond one's capacities.");
        question.setProperty("c", "c) to plan carefully for the future.");
        question.setProperty("d", "d) to do well what needs to be done in the present.");
        question.setProperty("correct", "d");
        datastore.put(question);

        question = new Entity("Quiz", 2);
        question.setProperty("chinese", "2. 請 禮 讓 殘 障 同 胞 孕 婦 及 70 歲 以 上 老 人 優 先 購 票 。");
        question.setProperty("instruction", "(This sign is posted in the Taipei Railway Station.)");
        question.setProperty("question", "What is included in the message?");
        question.setProperty("a", "a) Be courteous to your fellow-travelers.");
        question.setProperty("b", "b) Assist pregnant women.");
        question.setProperty("c", "c) Let the handicapped buy tickets first.");
        question.setProperty("d", "d) Discount tickets available to people over 70 years of age.");
        question.setProperty("correct", "c");
        datastore.put(question);

        question = new Entity("Quiz", 3);
        question.setProperty("chinese", "3. 幾 年 前 ， 當 我 從 香 港 、 日 本 拍 片 回 來 ， 我 的 母 親 就 非 常 緊 張 ， 包 括 我 的 兄 妹 。 當 他 們 看 見 我 的 衣 著 和 舉 止 仍 像 過 去 那 麼 普 通 、 隨 便 ， 他 們 才 恢 復 了 常 態 。");
        question.setProperty("instruction", "(This passage was taken from a magazine article.)");
        question.setProperty("question", "Why did her family members behave in the way described in this passage? ");
        question.setProperty("a", "a) They had not seen each other for years.");
        question.setProperty("b", "b) They were afraid that she might have changed.");
        question.setProperty("c", "c) They were worried about her health.");
        question.setProperty("d", "d) They didn't like what she was wearing.");
        question.setProperty("correct", "b");
        datastore.put(question);

        question = new Entity("Quiz", 4);
        question.setProperty("chinese", "4. 我 們 把 你 的 來 信 告 訴 大 家 ， 希 望 讀 者 和 編 者 共 同 努 力 。");
        question.setProperty("instruction", "(This is an excerpt from a magazine editor's response to a reader's letter.) ");
        question.setProperty("question", "What hope does the editor express?");
        question.setProperty("a", "a) That the readers will be understanding.");
        question.setProperty("b", "b) That by working together, we can reach the objective.");
        question.setProperty("c", "c) That the magazine staff's efforts will be successful.");
        question.setProperty("d", "d) That readers will recognize the danger.");
        question.setProperty("correct", "b");
        datastore.put(question);

        question = new Entity("Quiz", 5);
        question.setProperty("chinese", "5. 加 藤 周 一 先 生 ﹕ 請 原 諒 我 冒 昧 地 給 您 寫 信 。 我 是 一 個 中 國 的 作 家 ， 今 年 滿 八 十 二 歲 ， 長 期 生 活 在 上 海 。");
        question.setProperty("instruction", "(This excerpt comes from a letter published in a magazine.)");
        question.setProperty("question", "Based on this letter's opening line, how would you describe the relationship between the receiver and the writer?");
        question.setProperty("a", "a) Husband and wife.");
        question.setProperty("b", "b) Teacher and student.");
        question.setProperty("c", "c) Employer and employee.");
        question.setProperty("d", "d) Two strangers.");
        question.setProperty("correct", "d");
        datastore.put(question);

        question = new Entity("Quiz", 6);
        question.setProperty("chinese", "6. 請 勿 蹲 踩 座 蓋 ， 保 持 清 潔 ， 多 謝 。");
        question.setProperty("instruction", "(This message is seen in a restroom.)");
        question.setProperty("question", "This sign is attached to the wall of a toilet . What is the message?");
        question.setProperty("a", "a) Do not climb onto the toilet seat.");
        question.setProperty("b", "b) Do not throw foreign objects into the toilet.");
        question.setProperty("c", "c) Flush after every use.");
        question.setProperty("d", "d) Throw used toilet paper into the waste receptable.");
        question.setProperty("correct", "a");
        datastore.put(question);

        question = new Entity("Quiz", 7);
        question.setProperty("chinese", "7. 我 國 是 一 個 人 口 大 國 ， 食 物 供 給 的 穩 定 與 否 ， 關 係 到 整 個 國 民 經 濟 的 發 展 和 社 會 的 安 定 。");
        question.setProperty("instruction", "(This passage is taken from a magazine article.)");
        question.setProperty("question", "What is the focus of this sentence?");
        question.setProperty("a", "a) Food supply.");
        question.setProperty("b", "b) Population. ");
        question.setProperty("c", "c) Economics. ");
        question.setProperty("d", "d) Social stability.");
        question.setProperty("correct", "a");
        datastore.put(question);

        question = new Entity("Quiz", 8);
        question.setProperty("chinese", "8. 從 中 正 機 場 到 台 北 市 區 最 快 捷 方 便 的 交 通 工 具 是 台 灣 汽 車 客 運 公 司 、 中 興 號 班 車 ， 每 十 五 到 二 十 分 鐘 一 班 車 ， 單 程 票 價 72 元 ， 車 程 約 50 分 鐘 。");
        question.setProperty("instruction", "(This is an excerpt from a guide to services provided at an airport in Taiwan.)");
        question.setProperty("question", "Which of the following is true? ");
        question.setProperty("a", "a) The bus to downtown Taipei takes approximately 15-20 minutes.");
        question.setProperty("b", "b) The bus runs every 50 minutes. ");
        question.setProperty("c", "c) The bus is operated by the Taiwan Transportation Company.");
        question.setProperty("d", "d) A round-trip ticket is 72 NT.");
        question.setProperty("correct", "c");
        datastore.put(question);

        question = new Entity("Quiz", 9);
        question.setProperty("chinese", "9. 女 兒 ﹕ 又 是 大 魚 大 肉 。 我 們 吃 膩 了！ 母 親 ﹕ 別 抱 怨 ！ 咱 們 吃 不 起 青 菜 ！ 父 親﹕ 唉 ！");
        question.setProperty("instruction", "(Captions to a cartoon portraying a family of four sitting around the kitchen table for dinner.) ");
        question.setProperty("question", "What is the message of this cartoon? ");
        question.setProperty("a", "a) Vegetables are expensive.");
        question.setProperty("b", "b) Fish and meat are expensive.");
        question.setProperty("c", "c) It is not healthy to eat only fish and meat.");
        question.setProperty("d", "d) It is boring to eat only vegetables.");
        question.setProperty("correct", "a");
        datastore.put(question);

        question = new Entity("Quiz", 10);
        question.setProperty("chinese", "10. 根 據 最 近 對 … 500 大 企 業 的 主 管 所 做 的 調 查 顯 示 ， 女 主 管 比 男 主 管 有 領 導 的 特 質 。");
        question.setProperty("instruction", "(This passage is taken from a newspaper report.)");
        question.setProperty("question", "The survey mentioned in this article suggests that: ");
        question.setProperty("a", "a) there is less gender discrimination against women now than before.");
        question.setProperty("b", "b) women have almost equal opportunities for promotion.");
        question.setProperty("c", "c) women are better leaders than their male counterparts.");
        question.setProperty("d", "d) female executives exercise authority the same way as their male counterparts.");
        question.setProperty("correct", "c");
        datastore.put(question);

        question = new Entity("Quiz", 11);
        question.setProperty("chinese", "11. 我 一 直 認 為 所 有 的 婦 女 都 是 職 業 婦 女 ， 因 此 「 職 業 婦 女 」 這 個 名 稱 是 沒 有 必 要 的 。 世 上 只 有 沒 有 職 業 的 男 人 ， 不 付 能 有 無 職 業 的 女 人 。");
        question.setProperty("instruction", "(This passage is taken from a newspaper article.) ");
        question.setProperty("question", "The author believes that the term \u201cworking woman\u201c:");
        question.setProperty("a", "a) gives proper recognition to working women.");
        question.setProperty("b", "b) is completely unnecessary, for all women work.");
        question.setProperty("c", "c) derides women who do not work outside their homes. ");
        question.setProperty("d", "d) implies hostility toward all working men.");
        question.setProperty("correct", "b");
        datastore.put(question);

        question = new Entity("Quiz", 12);
        question.setProperty("chinese", "12. 旅 館 ﹕ 從 豪 華 級 到 經 濟 實 惠 的 旅 館 ， 由 您 挑 選 ， 費 用 比 您 自 行 洽 訂 便 宜 。");
        question.setProperty("instruction", "(This is an excerpt from a hotel brochure.)");
        question.setProperty("question", "What does this passage say about hotel accommodations?");
        question.setProperty("a", "a) Locations are central.");
        question.setProperty("b", "b) All units are deluxe.");
        question.setProperty("c", "c) Rates are discounted.");
        question.setProperty("d", "d) Complimentary shuttle available.");
        question.setProperty("correct", "c");
        datastore.put(question);

        question = new Entity("Quiz", 13);
        question.setProperty("chinese", "13. 為 維 護 您 的 權 益 及 本 公 園 觀 瞻 ， 請 勿 向 流 動 攤 販 購買 物 品 。");
        question.setProperty("instruction", "(This sign is posted inside a park.)");
        question.setProperty("question", "What is the message?");
        question.setProperty("a", "a) Please visit out gift shop.");
        question.setProperty("b", "b) Do not litter. ");
        question.setProperty("c", "c) Do not patronize vendors.");
        question.setProperty("d", "d) No pets allowed in the park. ");
        question.setProperty("correct", "c");
        datastore.put(question);

        question = new Entity("Quiz", 14);
        question.setProperty("chinese", "14. 各 位 同 學 ﹕ 本 餐 廳 二 、 三 日 休 假 ， 四 日 起 恢 復 營 業 ， 祝 大 家 春 節 愉 快 。 新 新 餐 廳 啟");
        question.setProperty("instruction", "(This notice is posted outside a restaurant.)");
        question.setProperty("question", "What is the main point of this announcement?");
        question.setProperty("a", "a) Grand opening soon");
        question.setProperty("b", "b) Happy holidays");
        question.setProperty("c", "c) Closed for two days");
        question.setProperty("d", "d) Under new management ");
        question.setProperty("correct", "c");
        datastore.put(question);

        question = new Entity("Quiz", 15);
        question.setProperty("chinese", "15. 禁 止 在 餐 廳 內 食 用 非 本 公 司 產 品 。");
        question.setProperty("instruction", "(This sign is posted inside a building.)");
        question.setProperty("question", "What is the message?");
        question.setProperty("a", "a) No eating inside this compound.");
        question.setProperty("b", "b) No outside food allowed.");
        question.setProperty("c", "c) No smoking allowed.");
        question.setProperty("d", "d) No eating on company time.");
        question.setProperty("correct", "b");
        datastore.put(question);

        question = new Entity("Quiz", 16);
        question.setProperty("chinese", "16. 牛 乳 、 咖 啡 、 可 可 亞 等 先 用 開 水 沖 好 待 會 兒 再 加 蜂 蜜 以 免 損 壞 蜂 蜜 特 有 的 天 然 活 性 維 他 命 。");
        question.setProperty("instruction", "(The above instructions are included with a box of honey.)");
        question.setProperty("question", "According to these instructions, how might the nutritional value of honey be preserved? By adding it:");
        question.setProperty("a", "a) to a drink which has been allowed to cool.");
        question.setProperty("b", "b) to a powdered drink before adding boiling water.");
        question.setProperty("c", "c) first to boiling water, then to powdered drinks.");
        question.setProperty("d", "d) first to liquid vitamins, then to boiling water.");
        question.setProperty("correct", "a");
        datastore.put(question);

        question = new Entity("Quiz", 17);
        question.setProperty("chinese", "17. 我 拍 的 片 子 大 多 是 商 業 性的 ， 難 度 很 大 ， 有 時 一 個 鏡 頭 要 反 復 幾 次 ， 幾 十 次 ， 受 傷 更 是 避 免 不 了 的 。");
        question.setProperty("instruction", "(This passage was taken from a magazine article.) ");
        question.setProperty("question", "The person speaking is most likely:");
        question.setProperty("a", "a) a photographer.");
        question.setProperty("b", "b) a salesperson.");
        question.setProperty("c", "c) a reporter.");
        question.setProperty("d", "d) an actor.");
        question.setProperty("correct", "d");
        datastore.put(question);

        question = new Entity("Quiz", 18);
        question.setProperty("chinese", "18. 不 少 老 年 人 退 休 下 來，就 與 原 來 的 生 活 和 工 作 環 境 疏 遠，而 把 自 己 局 限 於 一 個 小 天 地，過 著 孤 獨 的 生 活。");
        question.setProperty("instruction", "(This is an excerpt from an article.)");
        question.setProperty("question", "What is the focus of this passage?");
        question.setProperty("a", "a) An isolated life style.");
        question.setProperty("b", "b) Early retirement.");
        question.setProperty("c", "c) The working environment.");
        question.setProperty("d", "d) The aging process.");
        question.setProperty("correct", "a");
        datastore.put(question);

        question = new Entity("Quiz", 19);
        question.setProperty("chinese", "19. 1417 室 XXX 同 學 今 天 1/7 有 你 的 掛 號 信 ， 請 帶 學 生 證 及 私 章 ， 速 來 領 取 ， 超 過 七 天 不 來 ， 即 退 回 原 寄 處 。");
        question.setProperty("instruction", "(This is a notice posted in a dormitory office.)");
        question.setProperty("question", "This notice instructs you to:");
        question.setProperty("a", "a) bring your identification card to the post office in order to retrieve a parcel.");
        question.setProperty("b", "b) pick up your accumulated mail from your mail box.");
        question.setProperty("c", "c) bring your identification card to the dormitory office to retrieve a registered letter.");
        question.setProperty("d", "d) bring your identification card to the post office to pick up a parcel that has been returned to you.");
        question.setProperty("correct", "c");
        datastore.put(question);

        question = new Entity("Quiz", 20);
        question.setProperty("chinese", "20. 服 務 指 南 ﹕ 禁 止 攜 帶 槍 枝 進 入 賓 館 ； 未 經 允 許 禁 止 使 用 自 帶 的 電 器 設 備 。");
        question.setProperty("instruction", "(Excerpt from hotel room regulations.)");
        question.setProperty("question", "Which of the following violates hotel policy?");
        question.setProperty("a", "a) Playing music loudly after midnight.");
        question.setProperty("b", "b) Smoking in the room.");
        question.setProperty("c", "c) Using one's own electrical appliances in the room.");
        question.setProperty("d", "d) Cooking in the room.");
        question.setProperty("correct", "c");
        datastore.put(question);

        resp.sendRedirect("/admin/quizdatastored.jsp");
    }
}
